package root.electronclass.electronclass.grade.widget

import android.content.Context
import android.graphics.Bitmap
import android.graphics.Canvas
import android.graphics.Paint
import android.graphics.Path
import android.graphics.RectF
import android.util.AttributeSet
import android.view.View
import java.text.DecimalFormat
import kotlin.random.Random
import androidx.core.graphics.createBitmap
import root.electronclass.electronclass.R

/**
 *
 * @author Lokiy
 * @date 2023/3/11 13:08
 */
class HomeworkElapsedChart : View {

    private var totalNumber = 1000f
    private val elapsedTimeArray = mutableListOf<Pair<String, Float>>()

    private val paint = Paint().apply {
        isAntiAlias = true
        style = Paint.Style.FILL
        textSize = 16f
    }
    private val rectF = RectF()
    private val path = Path()

    //进度条到左边的距离
    private val progressLeft: Float = 50f

    //数字到进度条左边的距离
    private val numTextLeft = 10f

    //文字到左边界的距离
    private val titleTextLeft = 2f
    private val classNames = arrayOf("语文", "数学", "英语", "体育", "美术", "音乐", "科学")
    private val df = DecimalFormat("0.##")

    //左边文字的字体大小
    private val titleTextSize = 8f

    //数字的字体大小
    private val numTextSize = 11f

    constructor(context: Context) : this(context, null)
    constructor(context: Context, attrs: AttributeSet?) : this(context, attrs, 0)
    constructor(context: Context, attrs: AttributeSet?, attrStyle: Int) : super(context, attrs, 0) {
        if (isInEditMode) {
            classNames.forEach {
                elapsedTimeArray.add(it to Random.nextInt(totalNumber.toInt()).toFloat())
            }
        }
    }

    fun setData(list: List<Pair<String, Float>>?) {
        if (list.isNullOrEmpty()) return
        elapsedTimeArray.clear()
        elapsedTimeArray.addAll(list)
        totalNumber = list.maxOf { it.second } / 3f * 10
        invalidate()
    }

    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)
        canvas ?: return
        val width = width.takeIf { it > 0 } ?: return
        val height = height

        val line = elapsedTimeArray.size
        val lineHeight = height * 1f / line
        val space = lineHeight / 3
        val progressWidth = width - progressLeft - space
        val halfSpace = space / 2
        val textDy = (lineHeight - space - titleTextSize) / 2 + halfSpace + titleTextSize
        val numDy = (lineHeight - space - numTextSize) / 2 + halfSpace + numTextSize

        elapsedTimeArray.forEachIndexed { i, it ->
            //科目
            paint.color = 0xFF071129.toInt()
            paint.textSize = titleTextSize
            canvas.drawText(it.first, titleTextLeft, lineHeight * i + textDy, paint)
            //时长
            paint.color = 0xFF000000.toInt()
            val text = df.format(it.second)
            paint.textSize = numTextSize
            canvas.drawText(text, progressLeft + numTextLeft, lineHeight * i + numDy, paint)
            //进度条
            paint.color = resources.getColor(R.color.c_brand, null)

            rectF.set(
                progressLeft,
                lineHeight * i + halfSpace,
                progressLeft + progressWidth * (it.second * 1f / totalNumber),
                lineHeight * (i + 1) - halfSpace
            )
//            canvas.drawRect(rectF, paint)

            val barLeft = progressLeft
            val barTop = lineHeight * i + halfSpace
            val barRight = progressLeft + progressWidth * (it.second * 1f / totalNumber)
            val barBottom = lineHeight * (i + 1) - halfSpace
            val triangleBase = (barBottom - barTop) / 2f      // 底边为高度的1/2

            path.apply {
                moveTo(barLeft, barTop)
                lineTo(barRight, barTop)
                if (barRight - barLeft > triangleBase) {
                    lineTo(barRight - triangleBase, barBottom)
                    lineTo(barLeft, barBottom)
                } else {
                    lineTo(barLeft, barBottom)
                }
                close()
            }
            canvas.drawPath(path, paint)
            path.reset()
            //时长
            paint.color = 0xFFFFFFFF.toInt()
            val bitmap = createTextBitmap(text, numDy)
            canvas.drawBitmap(bitmap, progressLeft, lineHeight * i, paint)

        }
    }

    private fun createTextBitmap(text: String, textDy: Float): Bitmap {
        val bitmap = createBitmap(rectF.width().toInt(), rectF.height().toInt() * 2)
        val cvs = Canvas(bitmap)
        cvs.drawText(text, numTextLeft, textDy, paint)
        cvs.save()
        return bitmap
    }
}